<?php

class Core_Agent_Domain extends Dcmp_Domain_Base{

	protected $name = 'agent';	
	
	public function loadComponent($name){
		$componentClass = $this->getName().'_component_'.$name.'_'.$name;
		$componentClass = Kd_Loader::loadClass($componentClass);
		$componentClass = empty($componentClass)?
                                'Core_Agent_Share_Component':
                                $componentClass;
		return new $componentClass($this);
	}	
	
	protected function initialize() {
        $themeName = 'default';
        $skinName = 'default';
        $templatePaths = array(DP_DESIGN . strtolower($this->getName()) . DS . $themeName . DS,
        					   DP_DESIGN . strtolower('frontend') . DS . $themeName . DS);
        $htmlPaths = array(DP_DESIGN . strtolower($this->getName()) . DS . $themeName . DS . 'html' . DS,
        				   DP_DESIGN . 'backend' . DS . 'default' . DS . 'html' . DS);
        
		$this->getConfig()->setSetting('themeName', $themeName);
		$this->getConfig()->setSetting('skinName', $skinName);
		$this->getConfig()->setSetting('templateDirectories', $templatePaths);
		
		$localizeCode = $this->getApplication()->getConfig()->getSetting('localize');
		$this->getConfig()->setSetting('localizeCode', $localizeCode);
        Kd_Html_Config::setTemplatePaths($htmlPaths);
        Kd_Localize_Config::setLocalizeCode($localizeCode);
        Kd_Localize_Factory::getTranslator()->loadFile('share', DP_LOCALIZE.$localizeCode.DS.'share.yml');
        Kd_Localize_Factory::getTranslator()->loadFile('agent', DP_LOCALIZE.$localizeCode.DS.'agent.yml');
        Kd_Localize_Factory::getTranslator()->loadFile('customer', DP_LOCALIZE.$localizeCode.DS.'customer.yml');
        Kd_Localize_Factory::getTranslator()->loadFile('status', DP_LOCALIZE.$localizeCode.DS.'status.yml');
        Kd_Localize_Factory::getTranslator()->loadFile('article', DP_LOCALIZE.$localizeCode.DS.'article.yml');
        Kd_Localize_Factory::getTranslator()->loadFile('promotion', DP_LOCALIZE.$localizeCode.DS.'promotion.yml');
        Kd_Localize_Factory::getTranslator()->loadFile('subject', DP_LOCALIZE.$localizeCode.DS.'subject.yml');
        Kd_Localize_Factory::getTranslator()->loadFile('permission', DP_LOCALIZE.$localizeCode.DS.'permission.yml');
		parent::initialize();
	}
	
	protected function authenticate() {
		$router = Kd_Web::getRouter();
		if($router->getComponent() != 'account'){
			$identity = Kd_Web::getIdentity();
			if(!$identity->isLogin()){
				Kd_Web::getResponse()->redirect($router->buildUrl('account/account/login'));
			}
		}
		parent::authenticate();
	}	
}